将数据库sql server image字段取出来保存为图片

您所在的位置:网站首页 sqlserver 修改字段 将数据库sql server image字段取出来保存为图片

将数据库sql server image字段取出来保存为图片

2023-06-12 19:02| 来源: 网络整理| 查看: 265

using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using XZL.ERP.Base.Common; using XZL.ERP.Base.Connection; using XZL.ERP.POS.Model.ViewModel.Mobile; using XZL.ERP.POS.Model.Weixin; using System.IO; using System.Drawing; using System.Xml.Linq;

namespace XZL.ERP.POS.DAL.Mobile {     public  class Scan_Dal : Base_DAL     {

        ///         /// 查询商品数据         ///         ///         public Result GetScandata(string code)         {             string str;             try             {                 StringBuilder sSQL = new StringBuilder();                 DataTable Com = new DataTable();                                   sSQL.Append("select   ImageID, ItemName,price,c1 ,IMG1.ItemCode ,itemImage,img1.ImageType from  OITM,IMG1" +                     " where  IMG1.ItemCode = OITM.ItemCode  and OITM.itemcode  = '" + code + "'");                 Com = PosDbContex.QueryToDataTable(sSQL.ToString(), null);                 //byte[] bytes = Convert.FromBase64String(Com.Rows[0]["itemImage"].ToString());

                //byte[] bytes = Encoding.Default.GetBytes( Com.Rows[0]["itemImage"].ToString());                 //MemoryStream memStream = new MemoryStream(bytes);                 //Image mImage = Image.FromStream(memStream);                 //memStream.Close();

                //创建命令对象,con参数是一个连接对象                 //SqlCommand com = new SqlCommand(sql, con);                 //con.Open();                 //SqlDataReader dr = com.ExecuteReader();                 //if (dr.Read())                 //{                 //    //声明数组,用于保存数据库的二进制数据                 //    byte[] mybyte = null;                 //    //读取数据保存到数组中                 //    mybyte = (byte[])dr["photo"];                 //    Image image;                 //    //读取数组数据成为文件流                 //    MemoryStream mymemorystream = new MemoryStream(mybyte);                 //    //转换成为图片格式。                 //    image = Image.FromStream(mymemorystream, true);                 //    this.pictureBox1.Image = image;                 //    mymemorystream.Close();  //关闭流                 //}

                MemoryStream ms = new MemoryStream();                 byte[] img = (byte[])Com.Rows[0]["itemImage"];                 MemoryStream memStream = new MemoryStream(img);                 Image mImage = Image.FromStream(memStream);                 Bitmap bp = new Bitmap(mImage);                 bp.Save("D:/img/"+DateTime.Now.ToString("yyyyMMddHHss") + ".jpg");//注意保存路径

                Result.Code= ResultCode.Success.GetHashCode();

                if (Com != null && Com.Rows.Count >0)                 {                     Result.Code = ResultCode.Success.GetHashCode();                     Result.Message = "成功";                     Result.Data = Com;                     return Result;                 }                 else                 {

                    Result.Message = "失败";                 }                 return Result;

                ///写到这里             }             catch (Exception ex)             {             //    Result.Code = ResultCode.Custom.GetHashCode();             //    GetAchieResult_M.Message = ex.Message;             //    GetAchieResult_M.Data = null;                 return Result;             }                      }

        ///         /// 查询数据         ///         ///         public string GetScandata1( string code)         {             string str;             try             {                 StringBuilder sSQL = new StringBuilder();                 Result resp = new Result();                 sSQL.Append("select top 1 * from OITM where ItemCode = '" + code + "'");                 str = PosDbContex.FirstOrDefault(sSQL.ToString(), null);                 return str;             }             catch (Exception ex)             {             //    Result.Code = ResultCode.Custom.GetHashCode();             //    GetAchieResult_M.Message = ex.Message;             //    GetAchieResult_M.Data = null;                 return "没有数据";             }                      }          public Boolean ChangCom(string Company, string Password)         {             try             {                 string sSQL="";                 sSQL = "update  COMC set isactive =1 where  companyname = @Company and DBname = @Password  ;";                 sSQL += "update  COMC set isactive =0 where  companyname != @Company ;";                 bool ret = PosDbContex.Execute(sSQL, new  { Company = Company, Password = Password });                 return ret;             }             catch (Exception ex)             {             //    Result.Code = ResultCode.Custom.GetHashCode();             //    GetAchieResult_M.Message = ex.Message;             //    GetAchieResult_M.Data = null;                 return false;             }                      }     } }  



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3